import requests
from lxml import etree


def get(url):
    response = requests.get(url)
    selector = etree.HTML(response.content)
    return selector.xpath('//div[@id="thumbs"]/section/ul/li/figure/img/@data-src')

for i in range(1, 11):
    url = "https://alpha.wallhaven.cc/search?q=&categories=001&purity=100&topRange=1M&sorting=toplist&order=desc&page={}".format(i)
    try:
        result = get(url)
        print("成功获取第{}页图片！".format(i))
    except:
        print("获取第{}页面失败！".format(i))
    for j in range(len(result)):
        try:
            response = requests.get(result[j])
            with open("e:\\python\\爬虫图片\\pic{}{}.jpg".format(i, j+1), "wb+") as f:
                f.write(response.content)
            print("成功下载第{}张图片！".format((i-1)*24+j+1))
        except:
            print("下载第{}页第{}张图片失败！".format(i, j+1))


